package com.yanqu.road.server.manager.cross.battle.dunhuangshiku;

import com.google.protobuf.AbstractMessage;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.dunhuangshiku.*;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eChatContentType;
import com.yanqu.road.entity.enums.eDunHuangShiKuEventType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.log.cross.LogDunHuangShiKuTeam;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.bussiness.player.activity.UserDunHuangShiKuActivityBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankCacheMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.TempCommonMgr;
import com.yanqu.road.server.manger.TempGoodsBagMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.BitUtil;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/15 17:53
 */
public class DunHuangShiKuMgr extends TempCrossMgr {

    public static Logger logger = LogManager.getLogger(DunHuangShiKuMgr.class.getName());

    /**
     * 协助记录最大id
     */
    public static AtomicLong maxRecordId;

    //    public static Map<Integer, DunHuangShiKuActivity> dunHuangShiKuActivityMap = new ConcurrentHashMap<>();
    public static Map<Integer, DunHuangShiKuActivityGroup> activityGroupMap = new ConcurrentHashMap<>();

    @Override
    public boolean reload() throws Exception {
        // 初始化 活动
        synchronized (this) {
            String nowTimeStr = DateHelper.getCurrentDateTimeString();
            List<Integer> activityTypeList = new ArrayList<>();
            activityTypeList.add(eActivityType.CrossDunHuangShiKuActivity.getValue());
            Map<Integer, ActivityInfo> tempActivityInfoMap =
                    ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
            for (ActivityInfo activityInfo : tempActivityInfoMap.values()) {
                Map<Integer, UnionActivityGroup> activityGroupMap = Cross2UnionActivityGroupMgr.getGroupMap(activityInfo.getActivityId());
                if (null != activityGroupMap) {
                    for (UnionActivityGroup activityGroup : activityGroupMap.values()) {
                        if (activityGroup.getCrossServerId() == CrossBaseServer.getInstance().getServerId()) {
                            getActivity(activityInfo.getActivityId(), activityGroup.getGroupId());
                        }
                    }
                }
            }

//            for (ActivityInfo activityInfo : activityInfoMap.values()) {
//                DunHuangShiKuActivity activity = getDunHuangShiKuActivity(activityInfo);
//                activity.activityId = activityInfo.getActivityId();
//                activity.initActivityConfigInfo(activityInfo.getActivityId());
//            }
        }
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }


    @Override
    public boolean init() throws Exception {
        maxRecordId = new AtomicLong(UserDunHuangShiKuActivityBusiness.getMaxRecordId());
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static DunHuangShiKuActivity getActivityOrNull(int activityId, int groupId) {
        DunHuangShiKuActivityGroup activityGroup = activityGroupMap.get(activityId);
        if (activityGroup == null) {
            return null;
        }

        return activityGroup.getActivityOrNull(groupId);
    }

    public static DunHuangShiKuActivity getActivity(int activityId, int groupId) {
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            return null;
        }
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        if (activityInfo == null) {
            return null;
        }

        DunHuangShiKuActivityGroup activityGroup = getActivityGroup(activityId);
        return activityGroup.getActivity(groupId);
    }

    public static DunHuangShiKuActivityGroup getActivityGroup(int activityId) {
        DunHuangShiKuActivityGroup activityGroup = activityGroupMap.get(activityId);
        if (activityGroup == null) {
            synchronized (activityGroupMap) {
                activityGroup = activityGroupMap.get(activityId);
                if (activityGroup == null) {
                    activityGroup = new DunHuangShiKuActivityGroup(activityId);
                    activityGroupMap.put(activityId, activityGroup);
                }
            }
        }
        return activityGroup;
    }


    /**
     * 获取队伍排行榜
     *
     * @param activityId
     * @param userId
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetTeamRankListRespMsg.Builder getTeamRankList(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuGetTeamRankListRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamRankListRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        activityInfo.updateTeamRankList(false);
        int maxRank = getMaxRank(activityId);
        for (DunHuangShiKuTeam item : activityInfo.teamRankList) {
            if (maxRank-- <= 0 || item.bookNum <= 0) {
                break;
            }
            // 下发n个
            builder.addTeamList(builderDunHuangShiKuTeamRankEntity(item));
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (dunHuangShiKuUser != null) {
            DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (team != null) {
                if (team.rank <= getMaxRank(activityId)) {
                    builder.setMyRank(team.rank);
                } else {
                    builder.setMyRank(-1);
                }
                builder.setBookNum(team.bookNum);
            }
        }
        builder.setRet(0);
        return builder;
    }


    /**
     * 获取仇人列表
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetEnemyListRespMsg.Builder getEnemyList(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuGetEnemyListRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetEnemyListRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (null == dunHuangShiKuUser) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
                return builder;
            }
            for (Map.Entry<Long, Integer> entry : dunHuangShiKuUser.baseInfo.getEnemyInfoMap().entrySet()) {
                DunHuangShiKuProto.DunHuangShiKuEnemyDetailEntity.Builder enemyBuilder = DunHuangShiKuProto.DunHuangShiKuEnemyDetailEntity.newBuilder();
                DunHuangShiKuUser enemy = activityInfo.userMap.get(entry.getKey());
                DunHuangShiKuTeam team = activityInfo.teamMap.get(enemy.baseInfo.getTeamId());
                enemyBuilder.setEnemyId(entry.getKey());
                enemyBuilder.setEnemyInfo(PlayerBasePb.parsePlayerBaseTempMsg(enemy.baseInfo.getUserBaseInfo()));
                enemyBuilder.setRegionId(enemy.baseInfo.getRegionId());
                if (team != null) {
                    enemyBuilder.setTeamName(team.baseInfo.getName());
                }
                enemyBuilder.setTimes(entry.getValue());
                builder.addEnemyList(enemyBuilder);
            }
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 查看第三区域玩家信息集合
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetMaxRegionUserInfoRespMsg.Builder getMaxRegionUserInfoList(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuGetMaxRegionUserInfoRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetMaxRegionUserInfoRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (activityInfo) {
            for (DunHuangShiKuUser item : activityInfo.region3UserList) {
                builder.addUserList(DunHuangShiKuProto.MaxRegionUserInfoEntity.newBuilder().setUserId(item.baseInfo.getUserId())
                        .setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(item.baseInfo.getUserBaseInfo())));
            }
        }
        return builder;
    }

    /**
     * 查看当前队伍的排名和分数
     *
     * @param activityId
     * @param userId
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetTeamRankRespMsg.Builder getTeamRankInfo(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuGetTeamRankRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamRankRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        int rank = -1;
        int bookNum = 0;
        if (activityInfo != null) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (dunHuangShiKuUser != null) {
                DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
                if (team != null) {
                    rank = getTeamRank(activityInfo, team);
                    bookNum = team.bookNum;
                }
            }
        }
        builder.setRet(0);
        builder.setRank(rank);
        builder.setBookNum(bookNum);
        return builder;
    }

    /**
     * 退出活动
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static void exit(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuExitRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuExitRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            return;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            return;
        }
        dunHuangShiKuUser.inActivity = false;
        builder.setRet(0);
    }

    /**
     * 获取壁画信息 (进入壁画)
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuEnterMuralRespMsg.Builder getMuralInfo(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuEnterMuralRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuEnterMuralRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        builder.setReceivedMuralBit(dunHuangShiKuUser.baseInfo.getMuralBit());
        DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
        if (team != null) {
            // 获取队伍壁画信息
            for (Map.Entry<Integer, DunHuangShiKuMuralInfo> entry : team.baseInfo.getMuralInfoMap().entrySet()) {
                boolean showColorInfo = team.baseInfo.getCurFinishMuralId() < entry.getKey();
                builder.addMuralInfo(builderDunHuangShiKuRestorationOfMuralEntity(activityInfo, entry.getKey(), entry.getValue(), showColorInfo));
            }
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 获取队伍聊天列表
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetTeamChatListRespMsg.Builder getTeamChatList(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuGetTeamChatListRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamChatListRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
        if (null == team) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
            return builder;
        }
        builder.setChatList(team.chatList);
        synchronized (activityInfo) {
            for (DunHuangShiKuHelpRecordData item : activityInfo.getHelpRecordDataMap(team).values()) {
                // 当前正在求助的记录id
                builder.addRecordList(DunHuangShiKuProto.DunHuangShiKuHelpSimpleRecordEntity.newBuilder().setId(item.getId()).setRecordTime(item.getRecordTime()));
            }
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 队伍聊天
     *
     * @param activityId
     * @param msg
     * @return
     */
    public static void teamChat(int activityId, WorldMessageProto.WorldMessageTempMsg msg) {
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, msg.getUserId());
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            return;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(msg.getUserId());
        if (null == dunHuangShiKuUser) {
            return;
        }
        DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
        if (null == team) {
            return;
        }
        synchronized (activityInfo) {
            activityInfo.addTeamChat(team, dunHuangShiKuUser, 0, msg.getContentType(), msg.getContent(), System.currentTimeMillis() / 1000);
        }
    }

    /**
     * 恢复体力
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuRecoverEnergyRespMsg.Builder recoverEnergy(int activityId, long userId, int goodsId, int num) {
        DunHuangShiKuProto.DunHuangShiKuRecoverEnergyRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuRecoverEnergyRespMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setGoodsId(goodsId);
        builder.setNum(num);
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            activityInfo.refreshDunHuangShiKuUser(dunHuangShiKuUser, false);
            GoodsInfo goodsInfo = TempCommonMgr.getGoodsInfo(activityInfo.DUNHUANGSHIKU_ENERGY_ITEM_ID);
            dunHuangShiKuUser.baseInfo.setEnergy(dunHuangShiKuUser.baseInfo.getEnergy() + (num * goodsInfo.getParamList().get(0).intValue()));
            builder.setEnergy(dunHuangShiKuUser.baseInfo.getEnergy());
            builder.setEnergyLastTime(dunHuangShiKuUser.baseInfo.getEnergyLastTime());
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 打开宝箱
     *
     * @param activityId
     * @param userId
     * @param boxId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuOpenBoxToGameServerMsg.Builder openBox(int activityId, long userId, int boxId, int num) {
        DunHuangShiKuProto.DunHuangShiKuOpenBoxToGameServerMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuOpenBoxToGameServerMsg.newBuilder();
        builder.setBoxId(boxId);
        builder.setNum(num);
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            int boxNum = dunHuangShiKuUser.baseInfo.getBoxMap().getOrDefault(boxId, 0);
            if (boxNum < num) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_NOT_YOUR_BOX);
                return builder;
            }
            // 扣宝箱
            dunHuangShiKuUser.baseInfo.getBoxMap().put(boxId, boxNum - num);
            dunHuangShiKuUser.baseInfo.setOp(DataStatus.Update);
            builder.setRegionId(dunHuangShiKuUser.baseInfo.getRegionId());
            builder.setTeamId(dunHuangShiKuUser.baseInfo.getTeamId());
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 领取队友互助奖励/协助攻击
     *
     * @param activityId
     * @param userId
     * @param recordId
     */
    public static DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationToGameServerMsg.Builder helpRecordOperation(int activityId, long userId, int type, long recordId, long time) {
        DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationToGameServerMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationToGameServerMsg.newBuilder();
        DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationRespMsg.Builder toClientMsg = DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            toClientMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            builder.setRespClient(toClientMsg);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            builder.setRespClient(toClientMsg);
            return builder;
        }
        DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
        if (null == team) {
            toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
            builder.setRespClient(toClientMsg);
            return builder;
        }
        synchronized (activityInfo) {
            if (type == 0) {
                // 领取奖励
                Map<Long, DunHuangShiKuHelpRecordData> map = dunHuangShiKuUser.getHelpRecordDataMap(false);
                if (map == null) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HELP_RECORD_NO_FOUND);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                DunHuangShiKuHelpRecordData record = map.get(recordId);
                if (record == null) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HELP_RECORD_NO_FOUND);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                if (record.getKilledUserId() != userId) {
                    // 未被击杀
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_INCONFORMITY_REWARD_CONDITION);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                if (record.isReceived()) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CANNOT_BE_REPEATED_REWARD);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                DunHuangShiKuEventConfig eventConfig = activityInfo.eventConfigMap.get(record.getEventId());
                if (eventConfig == null) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CONFIG_ERROR);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                long targetId = record.getTargetId();
                if (eventConfig.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                    targetId = record.getTargetId();
                } else if (eventConfig.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue()) {
                    targetId = eventConfig.getTargetId();
                }
                // 加珍贵文书
                activityInfo.addBookNum(dunHuangShiKuUser, record.getBookNum(), 4, targetId, eventConfig.getId());
                toClientMsg.setBookNum(record.getBookNum());
                // 领奖
                toClientMsg.setReward(record.getReward());
                record.setIsReceived(true);
                for (Map.Entry<Long, DunHuangShiKuAttackInfo> entry : record.getHelpAttackMap().entrySet()) {
                    if (record.getUserId() != entry.getKey()) {
                        toClientMsg.addHelpAttackList(builderDunHuangShiKuHelpAttackEntity(activityInfo, entry.getKey(), entry.getValue()));
                    }
                }
            } else {
                // 攻击
                Map<Long, DunHuangShiKuHelpRecordData> map = activityInfo.getHelpRecordDataMap(team);
                DunHuangShiKuHelpRecordData record = map.get(recordId);
                if (record == null) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HELP_RECORD_NO_FOUND);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                // 比较记录时间
                if (record.getRecordTime() != time) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HELP_RECORD_NO_FOUND);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                if (record.getKilledUserId() == -1) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HELP_RECORD_INVALID);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                if (record.getKilledUserId() != 0) {
                    // 未被击杀
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HAS_BEEN_KILLED);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                if (getCanHelpTimes(dunHuangShiKuUser, activityInfo) <= 0) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_NO_HELP_TIMES);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                // 是否是自己的事件 是就不需要加协助伤害值
                long now = System.currentTimeMillis() / 1000;
                DunHuangShiKuAttackInfo attackInfo = record.getHelpAttackMap().get(dunHuangShiKuUser.baseInfo.getUserId());
                if (attackInfo != null && now < (attackInfo.getAttackTime() + activityInfo.DUNHUANGSHIKU_ATTACK_CD)) {
                    toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_ATTACK_CD);
                    builder.setRespClient(toClientMsg);
                    return builder;
                }
                // 日志
                DunHuangShiKuProto.DunHuangShiKuMuralActionLogEntity.Builder log = DunHuangShiKuProto.DunHuangShiKuMuralActionLogEntity.newBuilder();
                log.setRegionId(dunHuangShiKuUser.baseInfo.getRegionId());
                log.setTeamId(dunHuangShiKuUser.baseInfo.getTeamId());
                log.setStatus(1);
                DunHuangShiKuEventConfig eventConfig = activityInfo.eventConfigMap.get(record.getEventId());
                log.setEventType(eventConfig.getEventType());
                if (eventConfig.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                    log.setTargetId(record.getTargetId());
                } else {
                    log.setTargetId(eventConfig.getTargetId());
                }
                // 被协助的玩家
                DunHuangShiKuUser helpedUser = activityInfo.userMap.get(record.getUserId());
                List<Object> attackResult = activityInfo.attackHelpRecord(team, dunHuangShiKuUser, helpedUser, record, now);
                boolean isKill = (boolean) attackResult.get(0);
                long damage = (long) attackResult.get(1);
                long remainBlood = (long) attackResult.get(2);
                long score = (long) attackResult.get(3);
                log.setBeforeAttackBlood(remainBlood + damage);
                log.setAfterAttackBlood(remainBlood);
                if (isKill) {
                    if (record.getUserId() == userId) {
                        // 自己击杀求助信息
                        for (Map.Entry<Long, DunHuangShiKuAttackInfo> entry : record.getHelpAttackMap().entrySet()) {
                            if (record.getUserId() != entry.getKey()) {
                                toClientMsg.addHelpAttackList(builderDunHuangShiKuHelpAttackEntity(activityInfo, entry.getKey(), entry.getValue()));
                            }
                        }
                        // 加珍贵文书
                        long targetId = record.getTargetId();
                        int logType = 0;
                        if (eventConfig.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                            targetId = record.getTargetId();
                            logType = 2;
                        } else if (eventConfig.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue()) {
                            targetId = eventConfig.getTargetId();
                            logType = 3;
                        }


                        activityInfo.addBookNum(helpedUser, record.getBookNum(), logType, targetId, eventConfig.getId());

                        toClientMsg.setBookNum(record.getBookNum());
                        toClientMsg.setReward(record.getReward());
                        record.setIsReceived(true);
                    } else {
                        // 帮队友击杀 需要通知队友领取奖励
                        MessageHelper.sendPacket(helpedUser.baseInfo.getServerId(), helpedUser.baseInfo.getUserId(), new YanQuMessage(ClientProtocol.U_DUN_HUANG_SHI_KU_BROADCAST_CAN_RECEIVE_HELP_REWARD));
                    }
                    if (helpedUser.baseInfo.getAttackStatus() == 2 || (helpedUser.baseInfo.getEventId() == record.getEventId() && helpedUser.baseInfo.getTargetId() == record.getTargetId())) {
                        // 如果当前的记录是 处于当前探索中  移除事件
                        activityInfo.removeEvent(helpedUser, true);
                    }
                    addFinishEventNum(activityInfo, helpedUser, eventConfig.getId());
                }
                dunHuangShiKuUser.baseInfo.setHelpTimes(dunHuangShiKuUser.baseInfo.getHelpTimes() + 1);
                dunHuangShiKuUser.baseInfo.setAllScore(dunHuangShiKuUser.baseInfo.getAllScore() + score);
                toClientMsg.setLastAttackTime(now);
                toClientMsg.setIsKill(isKill);
                toClientMsg.setDamage(damage);
                toClientMsg.setRemainBlood(remainBlood);
                // 加积分
                toClientMsg.setScore(score);
                toClientMsg.setAllScore(dunHuangShiKuUser.baseInfo.getAllScore());
                toClientMsg.setLog(log);
            }
        }
        toClientMsg.setRet(0);
        builder.setRespClient(toClientMsg);
        return builder;
    }

    /**
     * 获取玩家记录
     *
     * @param activityId
     * @param userId
     * @param type       0队友互助 1敦煌日志
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetRecordListRespMsg.Builder getRecordList(int activityId, long userId, int type) {
        DunHuangShiKuProto.DunHuangShiKuGetRecordListRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetRecordListRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (null == team) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            if (type == 0) {
                // 队友互助
                Set<DunHuangShiKuHelpRecordData> set = new HashSet<>();
                // 正在求助
                Map<Long, DunHuangShiKuHelpRecordData> helpRecordMap = activityInfo.getHelpRecordDataMap(team);
                if (helpRecordMap != null) {
                    set.addAll(helpRecordMap.values());
                }
                // 未领取的
                Map<Long, DunHuangShiKuHelpRecordData> userRecordMap = dunHuangShiKuUser.getHelpRecordDataMap(false);
                if (userRecordMap != null) {
                    for (DunHuangShiKuHelpRecordData item : userRecordMap.values()) {
                        // 特殊代码 2022年4月12日14:26:48 修复数据用
                        DunHuangShiKuEventConfig huangShiKuEventConfig = activityInfo.eventConfigMap.get(item.getEventId());
                        if (huangShiKuEventConfig != null && huangShiKuEventConfig.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue() && item.getTargetId() <= 0 && item.getKilledUserId() <= 0) {
                            item.setKilledUserId(item.getUserId());
                        }

                        if (item.getKilledUserId() == dunHuangShiKuUser.baseInfo.getUserId() && !item.isReceived()) {
                            set.add(item);
                        }
                    }
                }
                for (DunHuangShiKuHelpRecordData item : set) {
                    builder.addHelpRecord(builderDunHuangShiKuHelpRecordEntity(activityInfo, item, dunHuangShiKuUser));
                }
            } else {
                // 敦煌日志
                // 公共的日志
                for (DunHuangShiKuKillLogData item : activityInfo.killLogList) {
                    if (item.getType() == 0 && item.getAttackerId() == userId && item.getRealKillId() != userId) {
                        // 过滤别人协助击杀的记录
                        continue;
                    }
                    builder.addPublicLog(builderDunHuangShiKuKillLogEntity(activityInfo, item));
                }
                // 与我相关的伤害日志
                // 刷新自己的日志
                List<DunHuangShiKuKillLogData> list = dunHuangShiKuUser.getUserLogDataList(false);
                if (list != null) {
                    for (DunHuangShiKuKillLogData item : list) {
                        builder.addPrivateLog(builderDunHuangShiKuKillLogEntity(activityInfo, item));
                    }
                }
            }
            builder.setRet(0);
        }
        int canHelpTimes = getCanHelpTimes(dunHuangShiKuUser, activityInfo);
        builder.setCanHelpTimes(canHelpTimes);
        return builder;
    }

    private static int getCanHelpTimes(DunHuangShiKuUser dunHuangShiKuUser, DunHuangShiKuActivity activityInfo) {
        int curHelpTimes = dunHuangShiKuUser.baseInfo.getHelpTimes();
        int consumeEnergy = dunHuangShiKuUser.baseInfo.getConsumeEnergy();
        int addTimes = 0;
        if (activityInfo.helpConsumeHp > 0) {
            addTimes = consumeEnergy / activityInfo.helpConsumeHp * activityInfo.helpAddTimes;
        }
        int helpTimes = activityInfo.initHelpTimes + addTimes - curHelpTimes;
        if (helpTimes > 0) {
            return helpTimes;
        }
        return 0;
    }

    /**
     * 行为操作
     *
     * @param activityId
     * @param userId
     * @param type       0攻击 1接受 2绕过 3标记 4下一个区域
     * @param style
     * @param regionId   下一个区域id
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuActionToGameServerMsg.Builder action(int activityId, long userId, int type, int style, int regionId) {
        DunHuangShiKuProto.DunHuangShiKuActionToGameServerMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuActionToGameServerMsg.newBuilder();
        builder.setType(type);
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (null == team) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            DunHuangShiKuEventConfig huangShiKuEventConfig = activityInfo.eventConfigMap.get(dunHuangShiKuUser.baseInfo.getEventId());
            if (type != 4 && huangShiKuEventConfig == null) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_EVENT_NOT_EVENT);
                return builder;
            }
            long now = System.currentTimeMillis() / 1000;
            DunHuangShiKuProto.DunHuangShiKuMuralActionLogEntity.Builder log = DunHuangShiKuProto.DunHuangShiKuMuralActionLogEntity.newBuilder();

            log.setRegionId(dunHuangShiKuUser.baseInfo.getRegionId());
            log.setTeamId(dunHuangShiKuUser.baseInfo.getTeamId());
            if (huangShiKuEventConfig != null) {
                log.setEventType(huangShiKuEventConfig.getEventType());
            }
            switch (type) {
                case 0:
                    // 攻击
                    if (huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.NpcFight.getValue() &&
                            huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.PlayerFight.getValue()) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CANNOT_ATTACK);
                        return builder;
                    }
                    if ((now - dunHuangShiKuUser.baseInfo.getLastAttackTime()) < activityInfo.DUNHUANGSHIKU_ATTACK_CD) {
                        // 判断攻击时间
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_ATTACK_CD);
                        return builder;
                    }
                    long damage = 0;
                    boolean isKill = false;
                    long addScore = 0;
                    long remainBlood = 0;
                    if (dunHuangShiKuUser.baseInfo.getAttackStatus() == 2) {
                        log.setStatus(1);
                        // 攻击被标记的事件
                        DunHuangShiKuHelpRecordData recordData = dunHuangShiKuUser.currentHelpRecordData;
                        long targetId = 0;
                        int logType = 0;
                        if (huangShiKuEventConfig.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                            targetId = recordData.getTargetId();
                            logType = 2;
                        } else {
                            targetId = huangShiKuEventConfig.getTargetId();
                            logType = 3;
                        }
                        log.setTargetId(targetId);
                        List<Object> attackResult = activityInfo.attackHelpRecord(team, dunHuangShiKuUser, dunHuangShiKuUser, recordData, now);
                        isKill = (boolean) attackResult.get(0);
                        damage = (long) attackResult.get(1);
                        remainBlood = (long) attackResult.get(2);
                        addScore = (long) attackResult.get(3);
                        if (isKill) {
                            for (Map.Entry<Long, DunHuangShiKuAttackInfo> entry : recordData.getHelpAttackMap().entrySet()) {
                                if (recordData.getUserId() != entry.getKey()) {
                                    builder.addHelpAttackList(builderDunHuangShiKuHelpAttackEntity(activityInfo, entry.getKey(), entry.getValue()));
                                }
                            }
                            recordData.setIsReceived(true);

                            activityInfo.addBookNum(dunHuangShiKuUser, recordData.getBookNum(), logType, targetId, huangShiKuEventConfig.getId());
                            builder.setBookNum(recordData.getBookNum());
                            builder.setReward(recordData.getReward());
                        }
                    } else {
                        // 攻击未标注的事件
                        if (dunHuangShiKuUser.baseInfo.getAttackStatus() == 0) {
                            dunHuangShiKuUser.baseInfo.setAttackStatus(1);
                        }
                        if (huangShiKuEventConfig.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue()) {
                            if (dunHuangShiKuUser.baseInfo.getTargetId() <= 0) {
                                // 被击杀 加个保护
                                activityInfo.removeEvent(dunHuangShiKuUser, true);
                                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HAS_BEEN_KILLED);
                                return builder;
                            }
                            log.setTargetId(huangShiKuEventConfig.getTargetId());
                            // 剩余血量
                            List<Object> list = activityInfo.attackResult(dunHuangShiKuUser.baseInfo.getTargetId(), dunHuangShiKuUser.baseInfo.getAttack(), huangShiKuEventConfig.getScore(), dunHuangShiKuUser.baseInfo.getTargetTotalBlood());
                            isKill = (boolean) list.get(0);
                            damage = (long) list.get(1);
                            remainBlood = (long) list.get(2);
                            addScore = (long) list.get(3);
                            dunHuangShiKuUser.baseInfo.setTargetId(remainBlood);
                            if (isKill) {
                                int bookNum = calKillNpcDropBookNum(huangShiKuEventConfig);
                                builder.setBookNum(bookNum);
                                activityInfo.addBookNum(dunHuangShiKuUser, bookNum, 3, huangShiKuEventConfig.getTargetId(), huangShiKuEventConfig.getId());

                                Property reward = getDunHuangShiKuEventConfigReward(huangShiKuEventConfig);
                                String rewardStr = PropertyHelper.parsePropertyToString(reward);
                                activityInfo.addKillNpcLog(dunHuangShiKuUser, dunHuangShiKuUser, huangShiKuEventConfig, bookNum, reward);
                                builder.setReward(rewardStr);
                            }
                        } else {
                            // 玩家事件
                            DunHuangShiKuUser defender = activityInfo.userMap.get(dunHuangShiKuUser.baseInfo.getTargetId());
                            if (defender.baseInfo.getRemainBlood() <= 0) {
                                // 被击杀 加个保护
                                activityInfo.removeEvent(dunHuangShiKuUser, true);
                                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HAS_BEEN_KILLED);
                                return builder;
                            }
                            log.setTargetId(defender.baseInfo.getUserId());
                            List<Object> list = activityInfo.attackResult(defender.baseInfo.getRemainBlood(), dunHuangShiKuUser.baseInfo.getAttack(), huangShiKuEventConfig.getScore(), dunHuangShiKuUser.baseInfo.getTargetTotalBlood());
                            isKill = (boolean) list.get(0);
                            damage = (long) list.get(1);
                            remainBlood = (long) list.get(2);
                            addScore = (long) list.get(3);
                            defender.baseInfo.setRemainBlood(remainBlood);
                            activityInfo.notifyRemainBlood(defender, dunHuangShiKuUser, damage);
                            if (isKill) {
                                int bookNum = calKillUserDropBookNum(activityInfo, defender);
                                builder.setBookNum(bookNum);

                                dunHuangShiKuUser.baseInfo.setCurKillNum(dunHuangShiKuUser.baseInfo.getCurKillNum() + 1);
                                dunHuangShiKuUser.baseInfo.setCurRobBookNum(dunHuangShiKuUser.baseInfo.getCurRobBookNum() + bookNum);

                                activityInfo.addBookNum(dunHuangShiKuUser, bookNum, 2, defender.baseInfo.getUserId(), huangShiKuEventConfig.getId());
                                Property reward = getDunHuangShiKuEventConfigReward(huangShiKuEventConfig);
                                String rewardStr = PropertyHelper.parsePropertyToString(reward);
                                builder.setReward(rewardStr);
                                // 尝试移除 标记
                                activityInfo.attemptRemoveCurHelpRecord(dunHuangShiKuUser, defender.baseInfo.getUserId(), true);

                                activityInfo.death(defender, dunHuangShiKuUser, dunHuangShiKuUser, damage, false, huangShiKuEventConfig.getId(), bookNum, rewardStr);

                            } else {
                                // 击杀不加入私有日志
                                activityInfo.addUserLog((byte) 1, defender, dunHuangShiKuUser, damage, huangShiKuEventConfig, null);
                            }
                        }
                    }
                    dunHuangShiKuUser.baseInfo.setLastAttackTime(now);
                    // 加 积分 奖励
                    dunHuangShiKuUser.baseInfo.setAllScore(dunHuangShiKuUser.baseInfo.getAllScore() + addScore);
                    builder.setScore(addScore);
                    builder.setAllScore(dunHuangShiKuUser.baseInfo.getAllScore());
                    builder.setDamage(damage);
                    builder.setEventId(huangShiKuEventConfig.getId());
                    builder.setBlood(remainBlood);

                    log.setBeforeAttackBlood(remainBlood + damage);
                    log.setAfterAttackBlood(remainBlood);
                    if (isKill) {
                        addFinishEventNum(activityInfo, dunHuangShiKuUser, huangShiKuEventConfig.getId());
                        activityInfo.removeEvent(dunHuangShiKuUser, true);
                    }
                    break;
                case 1:
                    // 接受
                    if (huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.Welfare.getValue() &&
                            huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.Box.getValue() &&
                            huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.SutraCave.getValue()) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CANNOT_ATTACK);
                        return builder;
                    }
                    if (huangShiKuEventConfig.getEventType() == eDunHuangShiKuEventType.SutraCave.getValue()) {
                        // 藏金洞事件 不需要额外处理 区服加一个mallId标识就行
                    } else if (huangShiKuEventConfig.getEventType() == eDunHuangShiKuEventType.Box.getValue()) {
                        // 获得宝箱
                        dunHuangShiKuUser.baseInfo.getBoxMap().put(huangShiKuEventConfig.getTargetId(),
                                dunHuangShiKuUser.baseInfo.getBoxMap().getOrDefault(huangShiKuEventConfig.getTargetId(), 0) + 1);
                    }
                    dunHuangShiKuUser.baseInfo.setAllScore(dunHuangShiKuUser.baseInfo.getAllScore() + huangShiKuEventConfig.getScore());
                    addFinishEventNum(activityInfo, dunHuangShiKuUser, huangShiKuEventConfig.getId());

                    Property reward = getDunHuangShiKuEventConfigReward(huangShiKuEventConfig);
                    builder.setReward(PropertyHelper.parsePropertyToString(reward));
                    builder.setScore(huangShiKuEventConfig.getScore());
                    builder.setAllScore(dunHuangShiKuUser.baseInfo.getAllScore());
                    builder.setEventId(huangShiKuEventConfig.getId());

                    // 移除事件
                    activityInfo.removeEvent(dunHuangShiKuUser, false);
                    break;
                case 2:
                    // 绕过
                    if (huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.NpcFight.getValue() &&
                            huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.PlayerFight.getValue()) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CANNOT_PASS);
                        return builder;
                    }
                    if (dunHuangShiKuUser.baseInfo.getAttackStatus() == 0) {
                        // 事件攻击才能跳过
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CANNOT_PASS);
                        return builder;
                    }
                    boolean isRemove1 = true;
                    if (dunHuangShiKuUser.currentHelpRecordData != null && dunHuangShiKuUser.currentHelpRecordData.getEventId() == dunHuangShiKuUser.baseInfo.getEventId() && dunHuangShiKuUser.currentHelpRecordData.getTargetId() == dunHuangShiKuUser.baseInfo.getTargetId()) {
                        // 标记中还存在就不移除
                        isRemove1 = false;
                    }
                    log.setTargetId(dunHuangShiKuUser.baseInfo.getTargetId());
                    activityInfo.removeEvent(dunHuangShiKuUser, isRemove1);
                    break;
                case 3:
                    // 标记
                    if (huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.NpcFight.getValue() &&
                            huangShiKuEventConfig.getEventType() != eDunHuangShiKuEventType.PlayerFight.getValue()) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_THE_EVENT_NOT_MARK);
                        return builder;
                    }
                    // 只有打了才能标记
                    if (dunHuangShiKuUser.baseInfo.getAttackStatus() == 0) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_MARK_AFTER_ATTACK);
                        return builder;
                    }
                    if (dunHuangShiKuUser.baseInfo.getAttackStatus() == 2) {
                        // 不能重复标记
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CANNOT_BE_REPEATED_MARK);
                        return builder;
                    }
                    // 即一个标记列表内，一位玩家仅会有一条记录
                    for (DunHuangShiKuUser member : team.memberMap.values()) {
                        if (member.currentHelpRecordData != null && member.currentHelpRecordData.getEventId() == dunHuangShiKuUser.baseInfo.getEventId() && member.currentHelpRecordData.getTargetId() == dunHuangShiKuUser.baseInfo.getTargetId()) {
                            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_THE_USER_FLAGGED);
                            return builder;
                        }
                    }
                    // 有可领取的协助奖励不能发起协助
                    if (hasReceiveHelpReward(dunHuangShiKuUser)) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_HAVE_HELP_REWARD);
                        return builder;
                    }
                    if (dunHuangShiKuUser.currentHelpRecordData == null) {
                        DunHuangShiKuHelpRecordData dunHuangShiKuHelpRecordData = new DunHuangShiKuHelpRecordData();
                        dunHuangShiKuHelpRecordData.setInsertOption();
                        dunHuangShiKuHelpRecordData.setId(DunHuangShiKuMgr.maxRecordId.incrementAndGet());
                        dunHuangShiKuHelpRecordData.setActivityId(activityId);
                        dunHuangShiKuHelpRecordData.setGroupId(groupId);
                        dunHuangShiKuHelpRecordData.setUserId(userId);
                        dunHuangShiKuUser.currentHelpRecordData = dunHuangShiKuHelpRecordData;
                        dunHuangShiKuUser.getHelpRecordDataMap(true).put(dunHuangShiKuHelpRecordData.getId(), dunHuangShiKuHelpRecordData);
                    } else {
                        // 替换当前标记
                        DunHuangShiKuEventConfig oldEventConfig = activityInfo.eventConfigMap.get(dunHuangShiKuUser.currentHelpRecordData.getEventId());
                        if (oldEventConfig.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                            DunHuangShiKuUser targetUser = activityInfo.userMap.get(dunHuangShiKuUser.currentHelpRecordData.getTargetId());
                            targetUser.getCanAttackUserSet().remove(dunHuangShiKuUser);
                        }
                        dunHuangShiKuUser.currentHelpRecordData.getHelpAttackMap().clear();
                        dunHuangShiKuUser.currentHelpRecordData.setUpdateOption();
                    }
                    dunHuangShiKuUser.currentHelpRecordData.setEventId(dunHuangShiKuUser.baseInfo.getEventId());
                    dunHuangShiKuUser.currentHelpRecordData.setTargetId(dunHuangShiKuUser.baseInfo.getTargetId());
                    dunHuangShiKuUser.currentHelpRecordData.setTargetTotalBlood(dunHuangShiKuUser.baseInfo.getTargetTotalBlood());
                    DunHuangShiKuAttackInfo attackInfo = new DunHuangShiKuAttackInfo(dunHuangShiKuUser.baseInfo.getLastAttackTime(), 0);
                    dunHuangShiKuUser.currentHelpRecordData.getHelpAttackMap().put(dunHuangShiKuUser.baseInfo.getUserId(), attackInfo);
                    dunHuangShiKuUser.currentHelpRecordData.setUpdateOption();
                    dunHuangShiKuUser.currentHelpRecordData.setRecordTime(now);
                    dunHuangShiKuUser.baseInfo.setAttackStatus(2);
                    String content = dunHuangShiKuUser.currentHelpRecordData.getId() + "|" + dunHuangShiKuUser.currentHelpRecordData.getEventId();
                    if (huangShiKuEventConfig.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                        DunHuangShiKuUser targetUser = activityInfo.userMap.get(dunHuangShiKuUser.currentHelpRecordData.getTargetId());
                        long serverId = targetUser.baseInfo.getServerId();
                        String targetName = targetUser.baseInfo.getUserBaseInfo().getNickName();
                        String decoration = targetUser.baseInfo.getUserBaseInfo().getDecoration();
                        content = content + "|" + serverId + "|" + targetName + "|" + decoration;
                    }
                    activityInfo.addTeamChat(team, dunHuangShiKuUser, 4, eChatContentType.Common.getContentType(), content, now);
                    activityInfo.broadcastHelpRecordAttackInfo(team.memberMap.values(), team);
                    log.setTargetId(dunHuangShiKuUser.currentHelpRecordData.getTargetId());
                    break;
                case 4:
                    if (regionId != dunHuangShiKuUser.baseInfo.getRegionId()) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_REGION_ID_ERROR);
                        return builder;
                    }
                    DunHuangShiKuRegionConfig nextRegionConfig = activityInfo.regionConfigMap.get(dunHuangShiKuUser.baseInfo.getRegionId() + 1);
                    if (nextRegionConfig == null) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_NOT_NEXT_REGION);
                        return builder;
                    }
                    boolean flag = false;
                    if (style == 0) {
                        DunHuangShiKuRegionConfig curRegionConfig = activityInfo.regionConfigMap.get(dunHuangShiKuUser.baseInfo.getRegionId());
                        int finishTimes = 0;
                        for (Integer eventId : curRegionConfig.eventList) {
                            finishTimes += dunHuangShiKuUser.baseInfo.getFinishEventMap().getOrDefault(eventId, 0);
                        }
                        flag = finishTimes >= curRegionConfig.eventNum;
                    } else {
                        // 道具通关 判断道具是否足够
                        flag = true;
                    }
                    if (!flag) {
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CONDITIONS_NOT_MET);
                        return builder;
                    }
                    boolean isRemove2 = true;
                    if (dunHuangShiKuUser.currentHelpRecordData != null && dunHuangShiKuUser.currentHelpRecordData.getEventId() == dunHuangShiKuUser.baseInfo.getEventId() && dunHuangShiKuUser.currentHelpRecordData.getTargetId() == dunHuangShiKuUser.baseInfo.getTargetId()) {
                        // 标记中还存在就不移除
                        isRemove2 = false;
                    }
                    activityInfo.removeEvent(dunHuangShiKuUser, isRemove2);

                    DunHuangShiKuMgr.clearFinishEvent(dunHuangShiKuUser);
                    dunHuangShiKuUser.baseInfo.setRegionId(nextRegionConfig.getId());
                    if (dunHuangShiKuUser.baseInfo.getRegionId() > dunHuangShiKuUser.baseInfo.getUnlockRegionId()) {
                        dunHuangShiKuUser.baseInfo.setUnlockRegionId(dunHuangShiKuUser.baseInfo.getRegionId());
                    }
                    if (dunHuangShiKuUser.baseInfo.getRegionId() == activityInfo.maxRegionId) {
                        // 加队伍分数
                        activityInfo.addBookNum(dunHuangShiKuUser, activityInfo.DUNHUANGSHIKU_ENTER_MAX_REGION_REWARD, 0, 0, 0);
                        dunHuangShiKuUser.baseInfo.setRemainBlood(dunHuangShiKuUser.baseInfo.getBlood());
                        dunHuangShiKuUser.enterMaxRegionTime = (int) now;
                        activityInfo.region3UserList.add(dunHuangShiKuUser);
                        // 广播
                        broadcastMemberMsg(activityInfo.region3UserList, true, ClientProtocol.U_DUN_HUANG_SHI_KU_MAX_REGION_USER_NUM, 0, DunHuangShiKuProto.BroadcastDunHuangShiKuMaxRegionUserNum.newBuilder().setMaxRegionUserNum(activityInfo.region3UserList.size()));
                        builder.setBookNum(activityInfo.DUNHUANGSHIKU_ENTER_MAX_REGION_REWARD);
                    }
                    break;
                default:
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_PARAM_ERROR);
                    return builder;
            }
            builder.setLog(log);
        }
        builder.setRet(0);
        return builder;
    }


    /**
     * 探索
     *
     * @param activityId
     * @param userId
     * @param boxKeyNumMap
     */
    public static DunHuangShiKuProto.DunHuangShiKuExploreToGameServerMsg.Builder explore(int activityId, long userId, Map<Integer, Integer> boxKeyNumMap) {
        DunHuangShiKuProto.DunHuangShiKuExploreToGameServerMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuExploreToGameServerMsg.newBuilder();
        DunHuangShiKuProto.DunHuangShiKuExploreRespMsg.Builder toClientMsg = DunHuangShiKuProto.DunHuangShiKuExploreRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            toClientMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            builder.setToClientMsg(toClientMsg);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (null == dunHuangShiKuUser) {
                toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
                builder.setToClientMsg(toClientMsg);
                return builder;
            }
            // 玩家所在队伍
            DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (null == team) {
                toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                builder.setToClientMsg(toClientMsg);
                return builder;
            }
            // npc血量未生成
            if (activityInfo.getNpcBloodMap().size() <= 0) {
                toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_NPC_BLOOD_NO_CREATE);
                builder.setToClientMsg(toClientMsg);
                return builder;
            }
            // 事件是否完成
            if (dunHuangShiKuUser.baseInfo.getEventId() != 0) {
                toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_EVENT_INCOMPLETE);
                builder.setToClientMsg(toClientMsg);
                return builder;
            }
            // 体力
            activityInfo.refreshDunHuangShiKuUser(dunHuangShiKuUser, true);
            DunHuangShiKuRegionConfig huangShiKuRegionConfig = activityInfo.regionConfigMap.get(dunHuangShiKuUser.baseInfo.getRegionId());
            if (dunHuangShiKuUser.baseInfo.getEnergy() < huangShiKuRegionConfig.getEnergy()) {
                toClientMsg.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_ENERGY_NOT_ENOUGH);
                builder.setToClientMsg(toClientMsg);
                return builder;
            }
            DunHuangShiKuEventConfig eventConfig = activityInfo.randomEventId(dunHuangShiKuUser, team, huangShiKuRegionConfig, boxKeyNumMap);
            if (eventConfig == null) {
                toClientMsg.setRet(GameErrorCode.E_GAME_ERROR);
                builder.setToClientMsg(toClientMsg);
                return builder;
            }
            builder.setOleValue(dunHuangShiKuUser.baseInfo.getEnergy());
            dunHuangShiKuUser.baseInfo.setEnergy(dunHuangShiKuUser.baseInfo.getEnergy() - huangShiKuRegionConfig.getEnergy());
            dunHuangShiKuUser.baseInfo.setConsumeEnergy(dunHuangShiKuUser.baseInfo.getConsumeEnergy() + huangShiKuRegionConfig.getEnergy());

            builder.setNewValue(dunHuangShiKuUser.baseInfo.getEnergy());
            if (eventConfig.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                DunHuangShiKuUser targetUser = activityInfo.userMap.get(dunHuangShiKuUser.baseInfo.getTargetId());
                dunHuangShiKuUser.baseInfo.setTargetTotalBlood(targetUser.baseInfo.getBlood());

                toClientMsg.setTargetInfo(PlayerBasePb.parsePlayerBaseTempMsg(targetUser.baseInfo.getUserBaseInfo()));
                // 剩余血量
                toClientMsg.setRemainBlood(targetUser.baseInfo.getRemainBlood());
                // 总血量
                toClientMsg.setBlood(dunHuangShiKuUser.baseInfo.getTargetTotalBlood());

                targetUser.getCanAttackUserSet().add(dunHuangShiKuUser);
            } else if (eventConfig.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue()) {
                long blood = activityInfo.getNpcBlood(eventConfig.getId());
                dunHuangShiKuUser.baseInfo.setTargetId(blood);
                dunHuangShiKuUser.baseInfo.setTargetTotalBlood(blood);
                toClientMsg.setBlood(dunHuangShiKuUser.baseInfo.getTargetId());
                toClientMsg.setRemainBlood(dunHuangShiKuUser.baseInfo.getTargetTotalBlood());
            }
            dunHuangShiKuUser.baseInfo.addEventNum(eventConfig.getId());
            toClientMsg.setEventId(eventConfig.getId());
            toClientMsg.setRet(0);
            builder.setRegionId(dunHuangShiKuUser.baseInfo.getRegionId());
            builder.setTeamId(dunHuangShiKuUser.baseInfo.getTeamId());
            builder.setEventType(eventConfig.getEventType());
            builder.setToClientMsg(toClientMsg);
        }
        return builder;
    }

    /**
     * 领取壁画奖励
     *
     * @param activityId
     * @param muralId
     * @param userId
     * @param getAll
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuReceiveMuralRewardRespMsg.Builder receiveMuralReward(int activityId, int muralId, long userId, boolean getAll) {
        DunHuangShiKuProto.DunHuangShiKuReceiveMuralRewardRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuReceiveMuralRewardRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            // 玩家所在队伍
            DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (null == team) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            if (!getAll) {
                // 判断是否完成
                if (team.baseInfo.getCurFinishMuralId() < muralId) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_MURAL_NO_COMPLETE);
                    return builder;
                }
                DunHuangShiKuMuralConfig dunHuangShiKuMuralConfig = activityInfo.muralConfigMap.get(muralId);
                int index = activityInfo.muralConfigList.indexOf(dunHuangShiKuMuralConfig);
                if (BitUtil.get(dunHuangShiKuUser.baseInfo.getMuralBit(), index)) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CANNOT_BE_REPEATED_REWARD);
                    return builder;
                }
                dunHuangShiKuUser.baseInfo.setMuralBit(BitUtil.set(dunHuangShiKuUser.baseInfo.getMuralBit(), index, true));
                builder.setReward(dunHuangShiKuMuralConfig.getReward());
                builder.setRet(0);
            } else {
                // 领取所有
                Property totalReward = new Property();
                for (int i = 1; i <= team.baseInfo.getCurFinishMuralId(); i++) {
                    DunHuangShiKuMuralConfig dunHuangShiKuMuralConfig = activityInfo.muralConfigMap.get(i);
                    if (dunHuangShiKuMuralConfig == null) {
                        continue;
                    }
                    int index = activityInfo.muralConfigList.indexOf(dunHuangShiKuMuralConfig);
                    if (!BitUtil.get(dunHuangShiKuUser.baseInfo.getMuralBit(), index)) {
                        dunHuangShiKuUser.baseInfo.setMuralBit(BitUtil.set(dunHuangShiKuUser.baseInfo.getMuralBit(), index, true));
                        Property reward = PropertyHelper.parseStringToProperty(dunHuangShiKuMuralConfig.getReward());
                        totalReward.addProperty(reward);
                    }
                }
                if (totalReward.isNothing()) {
                    return builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_MURAL_NO_COMPLETE);
                }
                builder.setReward(totalReward.toHelperString());
                builder.setRet(0);
            }

        }
        return builder;
    }

    /**
     * 修复壁画 （最后一幅画 可以无限修复）
     *
     * @param activityId
     * @param muralId
     * @param propId
     * @param num
     */
    public static DunHuangShiKuProto.DunHuangShiKuRestorationMuralsToGameServerMsg.Builder restorationOfMural(int activityId, int muralId, int propId, int num, long userId) {
        DunHuangShiKuProto.DunHuangShiKuRestorationMuralsToGameServerMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuRestorationMuralsToGameServerMsg.newBuilder();
        builder.setLockNum(num);
        builder.setLockPropId(propId);
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            // 玩家所在队伍
            DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (null == team) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            int realUseNum = 0;
            boolean finishAllMural = (team.baseInfo.getCurFinishMuralId() >= activityInfo.getMaxMuralId());
            if (!finishAllMural) {
                // 未完成所有的壁画
                int curMuralId = activityInfo.getCurMuralId(team);
                if (muralId != curMuralId) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_MURAL_ID_ERROR);
                    return builder;
                }
                DunHuangShiKuMuralInfo muralInfo = team.baseInfo.getMuralInfoMap().get(curMuralId);
                // 已上色的颜料数量
                int colouringNum = 0;
                if (muralInfo == null) {
                    muralInfo = new DunHuangShiKuMuralInfo();
                    team.baseInfo.getMuralInfoMap().put(curMuralId, muralInfo);
                    team.baseInfo.setUpdateOption();
                }
                colouringNum = muralInfo.getColourMap().getOrDefault(propId, 0);
                // 是否修复最后一幅壁画
                // 该壁画可该颜料的配置
                DunHuangShiKuMuralConfig dunHuangShiKuMuralConfig = activityInfo.muralConfigMap.get(muralId);
                int needColourNumConfig = dunHuangShiKuMuralConfig.needColourList.get(activityInfo.coloringIdList.indexOf(propId));
                // 当前所需颜料数量
                int needColourNum = needColourNumConfig - colouringNum;
                if (needColourNum <= 0) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_NOT_NEED_THE_COLOR);
                    return builder;
                }
                // 实际使用数量
                realUseNum = Math.min(needColourNum, num);
                if (realUseNum > 0) {
                    muralInfo.getColourMap().put(propId, colouringNum + realUseNum);
                    muralInfo.getMemberFinishMap().put(userId, muralInfo.getMemberFinishMap().getOrDefault(userId, 0) + realUseNum);
                    team.baseInfo.setOp(DataStatus.Update);
                }
                // 广播上色进度
                broadcastMemberMsg(team.memberMap.values(), true, ClientProtocol.U_DUN_HUANG_SHI_KU_BROADCAST_MEMBER_RESTORATION_OF_MURAL, userId,
                        DunHuangShiKuProto.DunHuangShiKuRestorationOfMuralProgressRateEntity.newBuilder().setMuralInfo(builderDunHuangShiKuRestorationOfMuralEntity(activityInfo, curMuralId, muralInfo, true)));
                if (needColourNum <= realUseNum) {
                    // 单个颜料满 尝试进入下一幅
                    activityInfo.nextMural(team, curMuralId);
                }
            } else {
                realUseNum = num;
            }
            // 上色壁画id
            builder.setFinishMuralId(team.baseInfo.getCurFinishMuralId());
            builder.setMuralId(muralId);
            builder.setRegionId(dunHuangShiKuUser.baseInfo.getRegionId());
            builder.setTeamId(dunHuangShiKuUser.baseInfo.getTeamId());
            builder.setNum(realUseNum);
            builder.setRet(0);
        }
        return builder;
    }

    /**
     * 获取队伍信息
     *
     * @param activityId
     * @param teamId
     * @param userId
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetTeamInfoRespMsg.Builder getTeamInfo(int activityId, long teamId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuGetTeamInfoRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamInfoRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuTeam team = activityInfo.teamMap.get((int) teamId);
            if (null == team) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            builder.setTeamInfo(builderDunHuangShiKuGetTeamEntity(activityInfo, team, false));
            builder.setRet(0);
        }
        return builder;
    }

    /**
     * 入队审核
     *
     * @param activityId
     * @param status       0表示同意 1表示拒绝 2 踢出队伍
     * @param targetUserId 目标玩家 （一键拒绝传0）
     * @param userId       操作玩家
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuTeamTeamMemberManageRespMsg.Builder teamMemberManage(int activityId, int status, long targetUserId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuTeamTeamMemberManageRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuTeamTeamMemberManageRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (null == dunHuangShiKuUser) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
                return builder;
            }
            DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (null == team) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            if (team.baseInfo.getUserId() != userId) {
                // 没有权限
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_NO_MANAGE_AUTHORITY);
                return builder;
            }
            if (team.baseInfo.getUserId() == targetUserId) {
                // 不能对自己操作
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_CAN_NOT_OPERATE_ON_ONESELF);
                return builder;
            }
            DunHuangShiKuUser targetUser = activityInfo.userMap.get(targetUserId);
            if (status == 0) {
                // 同意
                if (team.memberMap.size() >= 4) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_MEMBER_LIMIT);
                    return builder;
                }
                if (team.memberMap.containsKey(targetUserId)) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_ALREADY_IN_THE_TEAM);
                    return builder;
                }
                if (!team.getApplyJoinUserMap().containsKey(targetUserId)) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_PLAYER_NOT_ON_APPLY_LIST);
                    return builder;
                }
                if (targetUser == null) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TARGET_NO_EXIST);
                    return builder;
                }
                if (targetUser.baseInfo.getTeamId() != 0) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_EXISTING_TEAM);
                    return builder;
                }
                activityInfo.addMember(team, targetUser);
                // 取消该联盟申请
                activityInfo.cancelApply(targetUser, team, false);
                // 取消其他联盟申请
                activityInfo.cancelApply(targetUser, true);
                // 入队广播
                MessageHelper.sendPacket(targetUser.baseInfo.getServerId(), targetUser.baseInfo.getUserId(),
                        YanQuMessageUtils.buildMessage(ClientProtocol.U_DUN_HUANG_SHI_KU_BROADCAST_SUCCESS_JOIN_TEAM_INFO, builderDunHuangShiKuGetTeamEntity(activityInfo, team, false)));
            } else if (status == 1) {
                // 拒绝
                if (targetUser != null) {
                    activityInfo.cancelApply(targetUser, team, false);
                } else {
                    // 一键拒绝
                    activityInfo.cancelAllTeamApply(team);
                }
            } else {
                if (targetUser == null) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TARGET_NO_EXIST);
                    return builder;
                }
                // 踢出队伍
                if (targetUser.baseInfo.getTeamId() != team.baseInfo.getTeamId()) {
                    builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_PLAYER_NOT_IN_TEAM);
                    return builder;
                }
                activityInfo.memberQuitTeam(team, targetUser, 1);
            }
            builder.setRet(0);
        }
        return builder;
    }

    /**
     * 退出队伍
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuQuitTeamRespMsg.Builder quitTeam(int activityId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuQuitTeamRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuQuitTeamRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (null == dunHuangShiKuUser) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
                return builder;
            }
            DunHuangShiKuTeam dunHuangShiKuTeam = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
            if (null == dunHuangShiKuTeam) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            if (dunHuangShiKuUser.baseInfo.getUserId() == dunHuangShiKuTeam.baseInfo.getUserId()) {
                // 解散队伍
                StringBuilder memberSB = new StringBuilder();
                for (DunHuangShiKuUser item : dunHuangShiKuTeam.memberMap.values()) {
                    if (memberSB.length() > 0) {
                        memberSB.append("|");
                    }
                    memberSB.append(item.baseInfo.getUserId());
                    activityInfo.memberQuitTeam(dunHuangShiKuTeam, item, 2);
                }
                dunHuangShiKuTeam.leaderInfo = null;
                dunHuangShiKuTeam.baseInfo.setIsDelete(true);
                dunHuangShiKuTeam.chatList.clear();
                activityInfo.cancelAllTeamApply(dunHuangShiKuTeam);
                activityInfo.teamMap.remove(dunHuangShiKuTeam.baseInfo.getTeamId());
                activityInfo.teamList.remove(dunHuangShiKuTeam);
                synchronized (activityInfo.deleteList) {
                    activityInfo.deleteList.add(dunHuangShiKuTeam);
                }
                activityInfo.serverTeamsMap.get(dunHuangShiKuTeam.baseInfo.getUserServerId()).remove(dunHuangShiKuTeam);
                // 日志
                CrossAutoLogMgr.add(new LogDunHuangShiKuTeam(activityId, dunHuangShiKuTeam.baseInfo.getTeamId(), userId, 1, memberSB.toString()));
            } else {
                // 主动离开队伍
                activityInfo.memberQuitTeam(dunHuangShiKuTeam, dunHuangShiKuUser, 3);
            }
            builder.setRet(0);
        }
        return builder;
    }

    /**
     * 获取可申请的队伍信息
     *
     * @param activityId
     * @param page
     * @param type
     * @param userId     玩家id
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetTeamListRespMsg.Builder getCanApplyTeamList(int activityId, int page, int type, long userId) {
        DunHuangShiKuProto.DunHuangShiKuGetTeamListRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamListRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            List<DunHuangShiKuTeam> teamList = null;
            if (type == 0) {
                // 所有区服队伍
                teamList = activityInfo.teamList;
            } else {
                //分组跨服
                groupId = CrossUnionServerGroupMgr.getGroupIdByServerId(dunHuangShiKuUser.baseInfo.getServerId());
                Set<Long> serverSet = CrossUnionServerGroupMgr.getGroupServer(groupId);
                teamList = new ArrayList<>();
                for (long sid : serverSet) {
                    List<DunHuangShiKuTeam> list = activityInfo.serverTeamsMap.get(sid);
                    if(list != null){
                        teamList.addAll(list);
                    }
                }
                //teamList = activityInfo.serverTeamsMap.get(dunHuangShiKuUser.baseInfo.getServerId());
            }
            List<DunHuangShiKuTeam> tempList = new ArrayList<>();
            if (teamList != null) {
                for (DunHuangShiKuTeam team : teamList) {
                    if (dunHuangShiKuUser.baseInfo.getApplyJoinTeamMap().containsKey(team.baseInfo.getTeamId())) {
                        // 过滤申请的队伍列表
                        continue;
                    }
                    if (team.memberMap.size() < 4) {
                        tempList.add(team);
                    }
                }
            }
            tempList.sort((t1, t2) -> {
                long earn1 = activityInfo.getTeamEarn(t1);
                long earn2 = activityInfo.getTeamEarn(t2);
                if (earn1 != earn2) {
                    return Long.compare(earn2, earn1);
                } else {
                    return Long.compare(t1.baseInfo.getCreateTime(), t2.baseInfo.getCreateTime());
                }
            });
            page = Math.max(0, page);
            if (page == 0) {
                // 优先下发已申请的队伍
                for (int teamId : dunHuangShiKuUser.baseInfo.getApplyJoinTeamMap().keySet()) {
                    DunHuangShiKuTeam team = activityInfo.teamMap.get(teamId);
                    if (team != null) {
                        builder.addTeamList(builderDunHuangShiKuGetTeamEntity(activityInfo, team, false));
                    }
                }
            }
            int startIndex = (activityInfo.DUNHUANGSHIKU_GET_TEAM_LIST_PAGE_NUM * page);
            int endIndex = startIndex + activityInfo.DUNHUANGSHIKU_GET_TEAM_LIST_PAGE_NUM;
            for (int i = startIndex; i < endIndex; i++) {
                if (i > (tempList.size() - 1)) {
                    break;
                }
                builder.addTeamList(builderDunHuangShiKuGetTeamEntity(activityInfo, tempList.get(i), false));
            }
        }
        builder.setRet(0);
        return builder;
    }


    /**
     * 撤销申请
     *
     * @param activityId
     * @param teamId
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuCancelTeamApplyRespMsg.Builder cancelTeamApply(int activityId, int teamId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuCancelTeamApplyRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuCancelTeamApplyRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (null == dunHuangShiKuUser) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
                return builder;
            }
            DunHuangShiKuTeam applyTeam = activityInfo.teamMap.get(teamId);
            if (null == applyTeam) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            activityInfo.cancelApply(dunHuangShiKuUser, applyTeam, true);

            builder.setRet(0);
        }
        return builder;
    }

    /**
     * 申请加入队伍
     *
     * @param activityId
     * @param teamId
     * @param userId     申请加入的玩家
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuJoinTeamRespMsg.Builder applyJoinTeam(int activityId, int teamId, long userId) {
        DunHuangShiKuProto.DunHuangShiKuJoinTeamRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuJoinTeamRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        synchronized (activityInfo) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (null == dunHuangShiKuUser) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
                return builder;
            }
            if (dunHuangShiKuUser.baseInfo.getTeamId() != 0) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_EXISTING_TEAM);
                return builder;
            }
            DunHuangShiKuTeam team = activityInfo.teamMap.get(teamId);
            if (null == team) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NO_EXIST);
                return builder;
            }
            if (team.memberMap.size() >= 4) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_MEMBER_LIMIT);
                return builder;
            }
            if (team.memberMap.containsKey(userId)) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_ALREADY_IN_THE_TEAM);
                return builder;
            }
            long now = System.currentTimeMillis() / 1000;
            if (now <= dunHuangShiKuUser.baseInfo.getLastTimeLeaveTime() + activityInfo.DUNHUANGSHIKU_LEAVE_TEAM_CD) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_STILL_DURING_CD);
                return builder;
            }
            // 玩家申请上限
            if (dunHuangShiKuUser.baseInfo.getApplyJoinTeamMap().size() >= activityInfo.DUNHUANGSHIKU_USER_APPLY_MAX_LIMIT) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_APPLY_HAS_REACHED_THE_MAXIMUM);
                return builder;
            }
            // 队伍申请上限
            if (team.getApplyJoinUserMap().size() >= activityInfo.DUNHUANGSHIKU_APPLY_MAX_LIMIT) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_APPLY_HAS_REACHED_THE_MAXIMUM);
                return builder;
            }
            // 判断是否能够跨服组队
            if (activityInfo.DUNHUANGSHIKU_CAN_CROSS_TEAM_UP == 0) {
                int targetGroupId = CrossUnionServerGroupMgr.getGroupIdByServerId(team.baseInfo.getUserServerId());
                int applyGroupId = CrossUnionServerGroupMgr.getGroupIdByServerId(dunHuangShiKuUser.baseInfo.getServerId());
                if(targetGroupId != applyGroupId){
                    // 同区服组队
                    // 队长商会id
                    DunHuangShiKuUser leaderUser = activityInfo.userMap.get(team.baseInfo.getUserId());
                    if (team.baseInfo.getUserServerId() != dunHuangShiKuUser.baseInfo.getServerId() && ("".equals(leaderUser.baseInfo.getUserBaseInfo().getUnionUid()) || !dunHuangShiKuUser.baseInfo.getUserBaseInfo().getUnionUid().equals(leaderUser.baseInfo.getUserBaseInfo().getUnionUid()))) {
                        // 不同区服  又 不同商会
                        builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_NO_SUPPORT_CROSS_TEAM_UP);
                        return builder;
                    }
                }
            }
            dunHuangShiKuUser.baseInfo.getApplyJoinTeamMap().put(team.baseInfo.getTeamId(), now);
            dunHuangShiKuUser.baseInfo.setUpdateOption();
            team.getApplyJoinUserMap().put(dunHuangShiKuUser.baseInfo.getUserId(), dunHuangShiKuUser);
            // 通知队长
            DunHuangShiKuProto.DunHuangShiKuApplyChangeEntity.Builder changeBuilder = builderDunHuangShiKuApplyChangeEntity(0, builderDunHuangShiKuApplyUserEntity(dunHuangShiKuUser, now), null);
            MessageHelper.sendPacket(team.leaderInfo.baseInfo.getServerId(), team.leaderInfo.baseInfo.getUserId(),
                    YanQuMessageUtils.buildMessage(ClientProtocol.U_DUN_HUANG_SHI_KU_BROADCAST_TEAM_APPLY, changeBuilder));

            builder.setRet(0);
        }
        return builder;
    }

    /**
     * 创建队伍
     *
     * @param activityId
     * @param userId
     * @param name
     * @param hasEnoughMoneyIngots
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuCreateTeamRespMsg.Builder createTeam(int activityId, long userId, String name, boolean hasEnoughMoneyIngots) {
        DunHuangShiKuProto.DunHuangShiKuCreateTeamRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuCreateTeamRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (null == activityInfo) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
        if (null == dunHuangShiKuUser) {
            builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_USER_NOT_INIT);
            return builder;
        }
        synchronized (activityInfo) {
            if (dunHuangShiKuUser.baseInfo.getTeamId() != 0) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_EXISTING_TEAM);
                return builder;
            }
            if (activityInfo.checkTeamNameSame(name)) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_TEAM_NAME_SAME);
                return builder;
            }
            long now = System.currentTimeMillis() / 1000;
            if (now <= dunHuangShiKuUser.baseInfo.getLastTimeLeaveTime() + activityInfo.DUNHUANGSHIKU_LEAVE_TEAM_CD) {
                builder.setRet(GameErrorCode.E_DUN_HUANG_SHI_KU_STILL_DURING_CD);
                return builder;
            }
            if (dunHuangShiKuUser.baseInfo.getLastTimeCreateTime() > 0) {
                if (!hasEnoughMoneyIngots) {
                    builder.setRet(GameErrorCode.E_BAG_INGOTS_NO_ENOUGH);
                    return builder;
                }
            } else {
                builder.setIsFirstCreate(true);
            }
            // 撤回申请
            activityInfo.cancelApply(dunHuangShiKuUser, true);
            DunHuangShiKuTeam team = activityInfo.createDunHuangShiKuTeam(name, dunHuangShiKuUser);
            builder.setTeamInfo(DunHuangShiKuMgr.builderDunHuangShiKuGetTeamEntity(activityInfo, team, false));
            builder.setRet(0);
        }
        return builder;
    }

    /**
     * 获取敦煌石窟玩家数据(进入活动)
     *
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.Builder getDunHuangShiKuUserMsg(int activityId, long userId, UserBaseInfo userBaseInfo, long earnSpeed, long attack, long blood, boolean isEnter, List<DunHuangShiKuProto.DunHuangShiKuUserMallInfo> mallInfoList) {
        DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.newBuilder();
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (activityInfo == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        DunHuangShiKuUser dunHuangShiKuUser = null;
        DunHuangShiKuTeam team = null;
        synchronized (activityInfo) {
            dunHuangShiKuUser = activityInfo.getDunHuangShiKuUser(userId, userBaseInfo, earnSpeed, attack, blood);
            activityInfo.refreshDunHuangShiKuUser(dunHuangShiKuUser, false);
            // 队伍信息
            team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());

            if (isEnter) {
                dunHuangShiKuUser.inActivity = true;
                // 入队后是否进入过活动
                if (dunHuangShiKuUser.baseInfo.getTeamId() != 0) {
                    dunHuangShiKuUser.baseInfo.setIsEnter(true);
                }
                if (dunHuangShiKuUser.baseInfo.getLeaveInfo() != null) {
                    // 如果有离队信息，设完值重置信息
                    builder.setLeaveInfo(builderDunHuangShiKuLeaveInfoEntity(activityInfo, dunHuangShiKuUser));
                    dunHuangShiKuUser.baseInfo.setLeaveInfo(null);
                } else if (dunHuangShiKuUser.baseInfo.getEnemyInfo() != null) {
                    // 被击杀 仇人信息
                    builder.setEnemyInfo(builderDunHuangShiKuEnemyEntity(activityInfo, dunHuangShiKuUser));
                    dunHuangShiKuUser.baseInfo.setEnemyInfo(null);
                }
                builder.setMaxRegionUserNum(activityInfo.region3UserList.size());
            }
            builder.setUserInfo(builderDunHuangShiKuGetUserEntity(activityInfo, dunHuangShiKuUser));
            if (team != null) {
                boolean showApplyList = team.baseInfo.getUserId() == userId;
                builder.setTeamInfo(builderDunHuangShiKuGetTeamEntity(activityInfo, team, showApplyList));
            }
            builder.addAllMallInfoList(mallInfoList);
        }
        builder.setRet(0);
        return builder;
    }

//    /**
//     * 获取敦煌石窟活动
//     *
//     * @param activityInfo
//     * @return
//     */
//    public static synchronized DunHuangShiKuActivity getDunHuangShiKuActivity(ActivityInfo activityInfo, int groupId) {
//        DunHuangShiKuActivity dunHuangShiKuActivity = dunHuangShiKuActivityMap.get(activityInfo.getActivityId());
//        if (dunHuangShiKuActivity == null) {
//            // 读配置 获取活动信息
//            dunHuangShiKuActivity = new DunHuangShiKuActivity(activityInfo, groupId);
//            dunHuangShiKuActivityMap.put(activityInfo.getActivityId(), dunHuangShiKuActivity);
//        }
//        return dunHuangShiKuActivity;
//    }

    /**
     * 获取玩家在该活动的队伍排名
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static int getTeamRank(int activityId, long userId) {
        int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, userId);
        DunHuangShiKuActivity activityInfo = getActivity(activityId, groupId);
        if (activityInfo != null) {
            DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(userId);
            if (dunHuangShiKuUser != null) {
                DunHuangShiKuTeam team = activityInfo.teamMap.get(dunHuangShiKuUser.baseInfo.getTeamId());
                return getTeamRank(activityInfo, team);
            }
        }
        return -1;
    }

    private static int getTeamRank(DunHuangShiKuActivity activityInfo, DunHuangShiKuTeam team) {
        if (team != null) {
            activityInfo.updateTeamRankList(false);
            int maxRank = getMaxRank(activityInfo.getActivityId());
            if (team.rank <= maxRank && team.bookNum > 0) {
                return team.rank;
            }
        }
        return -1;
    }

    @Override
    public boolean save() {
        for (DunHuangShiKuActivityGroup activityGroup : activityGroupMap.values()) {
            activityGroup.save();
        }
        return true;
    }


    /**
     * 增加完成事件次数
     *
     * @param activity
     * @param dunHuangShiKuUser
     * @param eventId
     */
    public static void addFinishEventNum(DunHuangShiKuActivity activity, DunHuangShiKuUser dunHuangShiKuUser, int eventId) {
        if (dunHuangShiKuUser.baseInfo.getRegionId() != activity.maxRegionId) {
            dunHuangShiKuUser.baseInfo.getFinishEventMap().put(eventId,
                    dunHuangShiKuUser.baseInfo.getFinishEventMap().getOrDefault(eventId, 0) + 1);
            dunHuangShiKuUser.baseInfo.setOp(DataStatus.Update);
        }
    }

    /**
     * 清空完成事件次数
     *
     * @param dunHuangShiKuUser
     */
    public static void clearFinishEvent(DunHuangShiKuUser dunHuangShiKuUser) {
        dunHuangShiKuUser.baseInfo.getFinishEventMap().clear();
        dunHuangShiKuUser.baseInfo.setOp(DataStatus.Update);
    }

    /**
     * 获取玩家活动排名
     *
     * @param activityInfo
     * @param item
     * @return
     */
    public static int getUserRank(DunHuangShiKuActivity activityInfo, DunHuangShiKuUser item) {
        int activityId = activityInfo.getActivityId();
        int groupId = activityInfo.getGroupId();
        String idKey = String.valueOf(eGamePlayerEventType.DunHuangShiKuScoreCrossRank.getValue());
        RankProto.RankListMsg.Builder rankBuilder = CrossYRankCacheMgr.getUserRankList(activityId, groupId, idKey);
        return CrossYRankCacheMgr.getUserRank(activityId, groupId, idKey, rankBuilder, item.baseInfo.getUserId());
    }

    public static DunHuangShiKuProto.DunHuangShiKuLogEntity.Builder builderDunHuangShiKuKillLogEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuKillLogData item) {
        DunHuangShiKuProto.DunHuangShiKuLogEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuLogEntity.newBuilder();
        DunHuangShiKuUser attacker = activityInfo.userMap.get(item.getAttackerId());
        builder.setType(item.getType());
        builder.setAttackerId(item.getAttackerId());
        builder.setAttackerInfo(PlayerBasePb.parsePlayerBaseTempMsg(attacker.baseInfo.getUserBaseInfo()));
        builder.setKillType(item.getKillType());
        builder.setTargetId(item.getTargetId());
        builder.setTargetServerId(item.getTargetServerId());
        builder.setTargetUserName(item.getTargetName());
        builder.setDamage(item.getDamage());
        builder.setEventId(item.getEventId());
        builder.setLogTime(item.getLogTime());
        builder.setReward(item.getReward());
        builder.setBookNum(item.getBookNum());
        builder.setKillNum(item.getKillNum());
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuHelpRecordEntity.Builder builderDunHuangShiKuHelpRecordEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuHelpRecordData item, DunHuangShiKuUser user) {
        DunHuangShiKuProto.DunHuangShiKuHelpRecordEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuHelpRecordEntity.newBuilder();
        builder.setId(item.getId());
        DunHuangShiKuUser helper = activityInfo.userMap.get(item.getUserId());
        builder.setHelpId(helper.baseInfo.getUserId());
        builder.setHelpUserName(helper.baseInfo.getUserBaseInfo().getNickName());
        builder.setHelpServerId(helper.baseInfo.getServerId());
        builder.setTotalBlood(item.getTargetTotalBlood());
        DunHuangShiKuEventConfig config = activityInfo.eventConfigMap.get(item.getEventId());
        if (config.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue()) {
            builder.setBlood(item.getTargetId());
        } else {
            DunHuangShiKuUser target = activityInfo.userMap.get(item.getTargetId());
            builder.setTargetInfo(PlayerBasePb.parsePlayerBaseTempMsg(target.baseInfo.getUserBaseInfo()));
            if (item.getKilledUserId() == 0) {
                // 未被击杀 下发当前血量
                builder.setBlood(target.baseInfo.getRemainBlood());
            }
        }
        if (user.baseInfo.getUserId() == item.getKilledUserId() && !item.isReceived()) {
            builder.setHasReward(true);
        }
        builder.setEventId(item.getEventId());
        builder.setRecordTime(item.getRecordTime());

        DunHuangShiKuAttackInfo attackInfo = item.getHelpAttackMap().get(user.baseInfo.getUserId());
        if (attackInfo != null) {
            builder.setLastAttackTime(attackInfo.getAttackTime());
        }
        for (long helpId : item.getHelpAttackMap().keySet()) {
            // 过滤自己
            if (helpId != item.getUserId()) {
                builder.addHelpAttackNameList(activityInfo.userMap.get(helpId).baseInfo.getUserBaseInfo().getNickName());
            }
        }
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuGetTeamEntity.Builder builderDunHuangShiKuGetTeamEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuTeam team, boolean showApplyList) {
        DunHuangShiKuProto.DunHuangShiKuGetTeamEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamEntity.newBuilder();
        builder.setTeamId(team.baseInfo.getTeamId());
        builder.setTeamName(team.baseInfo.getName());
        builder.setLeaderId(team.baseInfo.getUserId());
        builder.setLeaderServerId(team.baseInfo.getUserServerId());
        builder.setCurMuralId(activityInfo.getCurMuralId(team));
        DunHuangShiKuUser dunHuangShiKuUser = activityInfo.userMap.get(team.baseInfo.getUserId());
        if (dunHuangShiKuUser != null) {
            builder.setLeaderName(dunHuangShiKuUser.baseInfo.getUserBaseInfo().getNickName());
        }
        long sumEarn = 0;
        if (team.memberMap != null) {
            for (DunHuangShiKuUser member : team.memberMap.values()) {
                builder.addMembers(builderMemberEntity(activityInfo, member));
                sumEarn += member.baseInfo.getCurPatronsEarn();
            }
        }
        builder.setSumEarn(sumEarn);
        builder.setCreateTime(team.baseInfo.getCreateTime());
        if (showApplyList) {
            for (DunHuangShiKuUser item : team.getApplyJoinUserMap().values()) {
                Long time = item.baseInfo.getApplyJoinTeamMap().get(team.baseInfo.getTeamId());
                if (time != null) {
                    builder.addApplyList(builderDunHuangShiKuApplyUserEntity(item, time));
                }
            }
        }
        builder.setFinishMuralId(team.baseInfo.getCurFinishMuralId());
        builder.setRank(getTeamRank(activityInfo, team));
        return builder;
    }

    /**
     * @param type      0加入/1撤销
     * @param applyInfo
     * @param userId
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuApplyChangeEntity.Builder builderDunHuangShiKuApplyChangeEntity(int type, DunHuangShiKuProto.DunHuangShiKuApplyUserEntity.Builder applyInfo, Long userId) {
        DunHuangShiKuProto.DunHuangShiKuApplyChangeEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuApplyChangeEntity.newBuilder();
        builder.setType(type);
        if (type == 0) {
            builder.setJoinInfo(applyInfo);
        } else if (type == 1) {
            builder.setUserId(userId);
        }
        return builder;
    }


    public static DunHuangShiKuProto.DunHuangShiKuApplyUserEntity.Builder builderDunHuangShiKuApplyUserEntity(DunHuangShiKuUser item, long time) {
        DunHuangShiKuProto.DunHuangShiKuApplyUserEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuApplyUserEntity.newBuilder();
        builder.setUserId(item.baseInfo.getUserId());
        builder.setEarnSpeed(item.baseInfo.getAttack());
        builder.setApplyTime(time);
        builder.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(item.baseInfo.getUserBaseInfo()));
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuMemberEntity.Builder builderMemberEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuUser item) {
        DunHuangShiKuProto.DunHuangShiKuMemberEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuMemberEntity.newBuilder();
        builder.setUserId(item.baseInfo.getUserId());
        builder.setMemberInfo(PlayerBasePb.parsePlayerBaseTempMsg(item.baseInfo.getUserBaseInfo()));
        builder.setRegionId(item.baseInfo.getRegionId());
        builder.setBookNum(item.baseInfo.getBookNum());
        builder.setAllScore(item.baseInfo.getAllScore());
        builder.setRank(getUserRank(activityInfo, item));
        builder.setServerId(item.baseInfo.getServerId());
        builder.setEarnSpeed(item.baseInfo.getCurPatronsEarn());
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuGetUserEntity.Builder builderDunHuangShiKuGetUserEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuUser item) {
        DunHuangShiKuProto.DunHuangShiKuGetUserEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetUserEntity.newBuilder();
        builder.setUserId(item.baseInfo.getUserId());
        builder.setUserName(item.baseInfo.getUserBaseInfo().getNickName());
        builder.setRegionId(item.baseInfo.getRegionId());
        builder.setUnlockRegionId(item.baseInfo.getUnlockRegionId());
        builder.setEnergy(item.baseInfo.getEnergy());
        builder.setEnergyLastTime(item.baseInfo.getEnergyLastTime());
        builder.setBlood(item.baseInfo.getRemainBlood());
        builder.setTotalBlood(item.baseInfo.getBlood());
        builder.setAttack(item.baseInfo.getAttack());
        builder.setEventId(item.baseInfo.getEventId());
        builder.setAttackStatus(item.baseInfo.getAttackStatus());
        builder.setIsEnter(item.baseInfo.isEnter());
        builder.setTeamId(item.baseInfo.getTeamId());
        builder.setTargetUserId(item.baseInfo.getTargetId());
        DunHuangShiKuEventConfig config = activityInfo.eventConfigMap.get(item.baseInfo.getEventId());
        if (config != null) {
            if (config.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
                // 总血量
                builder.setTargetTotalBlood(item.baseInfo.getTargetTotalBlood());
                DunHuangShiKuUser targetUser = activityInfo.userMap.get(item.baseInfo.getTargetId());
                builder.setTargetInfo(PlayerBasePb.parsePlayerBaseTempMsg(targetUser.baseInfo.getUserBaseInfo()));
                // 剩余血量
                builder.setTargetBlood(targetUser.baseInfo.getRemainBlood());

                builder.setTargetKillNum(targetUser.baseInfo.getCurKillNum());
                builder.setTargetRobBookNum(targetUser.baseInfo.getCurRobBookNum());
            } else if (config.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue()) {
                // 剩余血量
                builder.setTargetBlood(item.baseInfo.getTargetId());
                // 总血量
                builder.setTargetTotalBlood(item.baseInfo.getTargetTotalBlood());
            }
        }
        if (item.currentHelpRecordData != null) {
            builder.setCurrentHelp(builderDunHuangShiKuCurrentHelpEntity(activityInfo, item));
        }
        builder.setLastAttackTime(item.baseInfo.getLastAttackTime());
        builder.setAllScore(item.baseInfo.getAllScore());
        builder.setRank(getUserRank(activityInfo, item));
        builder.setBookNum(item.baseInfo.getBookNum());
        builder.setLastTimeLeaveTeam(item.baseInfo.getLastTimeLeaveTime());
        builder.setLastTimeCreateTeam(item.baseInfo.getLastTimeCreateTime());
        // 申请的队伍id
        builder.addAllApplyJoinTeamIdList(item.baseInfo.getApplyJoinTeamMap().keySet());
        for (Map.Entry<Integer, Integer> entry : item.baseInfo.getFinishEventMap().entrySet()) {
            builder.addFinishTimesMap(DunHuangShiKuProto.DunHuangShiKuFinishTimesTimesEntity.newBuilder()
                    .setEventId(entry.getKey()).setTimes(entry.getValue()));
        }
        // 宝箱信息
        for (Map.Entry<Integer, Integer> entry : item.baseInfo.getBoxMap().entrySet()) {
            builder.addBoxMap(DunHuangShiKuProto.DunHuangShiKuBoxEntity.newBuilder()
                    .setBoxId(entry.getKey()).setNum(entry.getValue()));
        }
        int enemyNum = 0;
        for (long enemyId : item.baseInfo.getEnemyInfoMap().keySet()) {
            DunHuangShiKuUser enemy = activityInfo.userMap.get(enemyId);
            if (enemy.baseInfo.getRegionId() == item.baseInfo.getRegionId()) {
                enemyNum++;
            }
        }
        builder.setEnemyNum(enemyNum);
        builder.setReceivedMuralBit(item.baseInfo.getMuralBit());
        builder.setIsCanReceiveHelpReward(hasReceiveHelpReward(item));

        builder.setUserKillNum(item.baseInfo.getCurKillNum());
        builder.setUserRobBookNum(item.baseInfo.getCurRobBookNum());
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuCurrentHelpEntity.Builder builderDunHuangShiKuCurrentHelpEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuUser dunHuangShiKuUser) {
        DunHuangShiKuProto.DunHuangShiKuCurrentHelpEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuCurrentHelpEntity.newBuilder();
        builder.setRecordId(dunHuangShiKuUser.currentHelpRecordData.getId());
        builder.setEventId(dunHuangShiKuUser.currentHelpRecordData.getEventId());
        builder.setTargetTotalBlood(dunHuangShiKuUser.currentHelpRecordData.getTargetTotalBlood());
        // 总血量
        DunHuangShiKuEventConfig config = activityInfo.eventConfigMap.get(dunHuangShiKuUser.currentHelpRecordData.getEventId());
        if (config.getEventType() == eDunHuangShiKuEventType.PlayerFight.getValue()) {
            DunHuangShiKuUser targetUser = activityInfo.userMap.get(dunHuangShiKuUser.currentHelpRecordData.getTargetId());
            if (targetUser != null) {
                builder.setTargetInfo(PlayerBasePb.parsePlayerBaseTempMsg(targetUser.baseInfo.getUserBaseInfo()));
                // 剩余血量
                builder.setTargetBlood(targetUser.baseInfo.getRemainBlood());
                builder.setUserId(dunHuangShiKuUser.currentHelpRecordData.getTargetId());
            }
        } else if (config.getEventType() == eDunHuangShiKuEventType.NpcFight.getValue()) {
            // 剩余血量
            builder.setTargetBlood(dunHuangShiKuUser.currentHelpRecordData.getTargetId());
        }
        return builder;
    }


    public static DunHuangShiKuProto.DunHuangShiKuLeaveInfoEntity.Builder builderDunHuangShiKuLeaveInfoEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuUser dunHuangShiKuUser) {
        DunHuangShiKuProto.DunHuangShiKuLeaveInfoEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuLeaveInfoEntity.newBuilder();
        builder.setType(dunHuangShiKuUser.baseInfo.getLeaveInfo().getLeaveStatus());
        builder.setTeamName(dunHuangShiKuUser.baseInfo.getLeaveInfo().getLeaveTeamName());
        DunHuangShiKuUser oldLeader = activityInfo.userMap.get(dunHuangShiKuUser.baseInfo.getLeaveInfo().getLeaveLeaderId());
        builder.setLeaderName(oldLeader.baseInfo.getUserBaseInfo().getNickName());
        builder.setLeaderServerId(oldLeader.baseInfo.getServerId());
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuEnemyEntity.Builder builderDunHuangShiKuEnemyEntity(DunHuangShiKuActivity activityInfo, DunHuangShiKuUser dunHuangShiKuUser) {
        DunHuangShiKuProto.DunHuangShiKuEnemyEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuEnemyEntity.newBuilder();
        DunHuangShiKuUser enemy = activityInfo.userMap.get(dunHuangShiKuUser.baseInfo.getEnemyInfo().getEnemyId());
        builder.setEnemyName(enemy.baseInfo.getUserBaseInfo().getNickName());
        builder.setEnemyServerId(enemy.baseInfo.getServerId());
        builder.setDamage(dunHuangShiKuUser.baseInfo.getEnemyInfo().getDamage());
        builder.setRobedBookNum(dunHuangShiKuUser.baseInfo.getEnemyInfo().getRobedBookNum());
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuTeamRankEntity.Builder builderDunHuangShiKuTeamRankEntity(DunHuangShiKuTeam team) {
        DunHuangShiKuProto.DunHuangShiKuTeamRankEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuTeamRankEntity.newBuilder();
        builder.setLeaderId(team.baseInfo.getUserId());
        builder.setLeaderName(team.leaderInfo.baseInfo.getUserBaseInfo().getNickName());
        builder.setLeaderServerId(team.baseInfo.getUserServerId());
        builder.setTeamId(team.baseInfo.getTeamId());
        builder.setBookNum(team.bookNum);
        builder.setTeamName(team.baseInfo.getName());
        builder.setRank(team.rank);
        return builder;
    }


    /**
     * @param activity
     * @param muralId
     * @param item
     * @return
     */
    public static DunHuangShiKuProto.DunHuangShiKuRestorationOfMuralEntity.Builder builderDunHuangShiKuRestorationOfMuralEntity(DunHuangShiKuActivity activity, int muralId, DunHuangShiKuMuralInfo item, boolean showColorInfo) {
        DunHuangShiKuProto.DunHuangShiKuRestorationOfMuralEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuRestorationOfMuralEntity.newBuilder();
        builder.setMuralId(muralId);
        // 只显示当前上色情况
        if (showColorInfo) {
            for (Map.Entry<Integer, Integer> entry : item.getColourMap().entrySet()) {
                builder.addColorInfo(DunHuangShiKuProto.DunHuangShiKuColorInfoEntity.newBuilder()
                        .setColorId(entry.getKey()).setNum(entry.getValue()).build());
            }
        }
        // 成员总贡献情况
        for (Map.Entry<Long, Integer> entry : item.getMemberFinishMap().entrySet()) {
            DunHuangShiKuUser member = activity.userMap.get(entry.getKey());
            DunHuangShiKuProto.DunHuangShiKuUserRestorationEntity.Builder restorationBuilder = DunHuangShiKuProto.DunHuangShiKuUserRestorationEntity.newBuilder();
            restorationBuilder.setColorNum(entry.getValue());
            restorationBuilder.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(member.baseInfo.getUserBaseInfo()));
            restorationBuilder.setUserId(entry.getKey());
            builder.addMemberInfo(restorationBuilder);
        }
        return builder;
    }

    public static DunHuangShiKuProto.DunHuangShiKuHelpAttackEntity.Builder builderDunHuangShiKuHelpAttackEntity(DunHuangShiKuActivity activityInfo, long userId, DunHuangShiKuAttackInfo attackInfo) {
        DunHuangShiKuProto.DunHuangShiKuHelpAttackEntity.Builder builder = DunHuangShiKuProto.DunHuangShiKuHelpAttackEntity.newBuilder();
        DunHuangShiKuUser helpUser = activityInfo.userMap.get(userId);
        if (helpUser != null) {
            builder.setHelperName(helpUser.baseInfo.getUserBaseInfo().getNickName());
            builder.setHelperServerId(helpUser.baseInfo.getServerId());
            builder.setDamage(attackInfo.getDamage());
        }
        return builder;
    }

    private static int getMaxRank(int activityId) {
        if (ActivityMgr.getActivityInfoMayLoadDB(activityId) == null) {
            // 因为导入活动 没有loadActivity 会进来
            return GameConfig.RANK_DEFAULT_MAX_COUNT;
        }
        return ActivityHelper.getMaxRankByConditionType(eGamePlayerEventType.DunHuangShiKuTeamScoreCrossRank.getValue(), ActivityMgr.getActivityConditionInfoList(activityId));
    }

    /**
     * 给玩家广播消息
     *
     * @param userList       玩家集合
     * @param needInActivity 是否需要在活动内
     * @param code
     * @param excludeId
     * @param builder
     */
    public static void broadcastMemberMsg(Collection<DunHuangShiKuUser> userList, boolean needInActivity, int code, long excludeId, AbstractMessage.Builder<?> builder) {
        for (DunHuangShiKuUser item : userList) {
            if ((!needInActivity || item.inActivity) && item.baseInfo.getUserId() != excludeId) {
                MessageHelper.sendPacket(item.baseInfo.getServerId(), item.baseInfo.getUserId(), YanQuMessageUtils.buildMessage(code, builder));
            }
        }
    }

    /**
     * 有可领取的协助奖励
     *
     * @param user
     * @return
     */
    public static boolean hasReceiveHelpReward(DunHuangShiKuUser user) {
        Map<Long, DunHuangShiKuHelpRecordData> map = user.getHelpRecordDataMap(false);
        if (map != null) {
            for (DunHuangShiKuHelpRecordData recordData : map.values()) {
                if (recordData.getKilledUserId() == user.baseInfo.getUserId() && !recordData.isReceived()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 计算玩家死亡掉落文书的数量
     *
     * @param dead
     * @return
     */
    public static int calKillUserDropBookNum(DunHuangShiKuActivity activity, DunHuangShiKuUser dead) {
        return activity.DUNHUANGSHIKU_LEAVE_MAX_REGION_PUNISH + dead.baseInfo.getCurRobBookNum() * activity.DUNHUANGSHIKU_DROP_BOOK_NUM_COEFFICIENT_1 / 100;
    }

    /**
     * 计算击杀npc获得文书数量
     *
     * @param config
     * @return
     */
    public static int calKillNpcDropBookNum(DunHuangShiKuEventConfig config) {
        if (new Random().nextInt(1000) < config.probability) {
            return config.num;
        }
        return 0;
    }

    /**
     * 计算获取配置中事件的奖励
     *
     * @param huangShiKuEventConfig
     * @return
     */
    public static Property getDunHuangShiKuEventConfigReward(DunHuangShiKuEventConfig huangShiKuEventConfig) {
        Property reward = PropertyHelper.parseStringToProperty(huangShiKuEventConfig.getReward());
        Property rewardProperty = new Property();
        if (reward != null) {
            for (Map.Entry<Integer, BigInteger> entry : reward.getGoods().entrySet()) {
                for (int i = 0; i < entry.getValue().intValue(); i++) {
                    OpenGoodsBagResult openGoodsBagResult = TempGoodsBagMgr.getRandomGoods(entry.getKey());
                    rewardProperty.addProperty(openGoodsBagResult.getReward());
                }
            }
        }
        return rewardProperty;
    }

    public boolean removeExpireData() {
        List<Integer> acList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for (DunHuangShiKuActivityGroup activityGroup : activityGroupMap.values()) {
            int activityId = activityGroup.getActivityId();
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            if(null == activityInfo){
                activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            }
            if (null != activityInfo && (nowTime > activityInfo.getEndShowTime() * 1000 + 3 * 24 * 3600 * 1000)) {
                acList.add(activityId);
            }
        }
        for (Integer activityId : acList) {
            activityGroupMap.remove(activityId);
        }
        return true;
    }


    /**
     * 获取需要获取玩家活跃数据的活动
     *
     * @return
     */
    public static synchronized Map<Integer, Set<Long>> getNeedSynActiveUserInfo() {
        Map<Integer, Set<Long>> activityServerList = new HashMap<>();
        for (DunHuangShiKuActivityGroup activityGroup : activityGroupMap.values()) {
            for (DunHuangShiKuActivity item : activityGroup.getActivityMap().values()) {
                List<Long> serverList = item.getNeedSynActiveUserInfo();
                int activityId = item.getActivityId();
                if (!serverList.isEmpty()) {
                    if (!activityServerList.containsKey(activityId)) {
                        activityServerList.put(activityId, new HashSet<>());
                    }
                    activityServerList.get(activityId).addAll(serverList);
                }
            }
        }
        return activityServerList;
    }

    /**
     * 接收到来自区服的活跃玩家数据
     *
     * @param activityId
     * @param groupId
     */
    public static void receiveActiveUserInfo(int activityId, long serverId, List<DunHuangShiKuProto.DunHuangShiKuActiveUserInfo> activeUserInfoList, int groupId){
        logger.info("接收到来自区服的活跃玩家数据activityId:{}, groupId:{}, serverId:{}, 活跃玩家数量：{}",activityId, groupId, serverId,activeUserInfoList.size());
        DunHuangShiKuActivity activity = getActivity(activityId, groupId);
        if (activity != null) {
            activity.receiveActiveUserInfo(serverId, activeUserInfoList);
        }
    }

    public static Map<Integer, DunHuangShiKuActivityGroup> getActivityGroupMap() {
        return activityGroupMap;
    }

    public static String getUserActivityUnionUid(int activityId, long userId) {
        return Cross2UnionActivityGroupMgr.getUnionUid(activityId, userId);
    }
}
